package KoebuBot;


use strict;
use warnings;
use lib '/root/.cpan/build/Net-Twitter-3.17001-NYTlxk/lib/';
use Net::Twitter::Lite;
use DBI();
#use YAML::Tiny;
binmode(STDOUT, ":utf8");
#my $config = ( YAML::Tiny->read('config.yml') );


sub new{
    my $class = shift;
    my $args = shift;
    my $twit = Net::Twitter::Lite->new(
        traits              => [qw/api::rest oauth/],
        consumer_key        => 'rPbsMD2RBOEBjVmBz8pbKg',
        consumer_secret     => 'MnfwIPnihjTkFNdf0WotUsCTiapwybvPGkB0w78Jg8',
        access_token        => '217277311-W7e80UAxyBTLF5kcSD0Ek2J5XHwTRyOMQTtJ3JTP',
        access_token_secret => 'LjpLDZ1YtxCNXJ4N6KKybcnmwOzxktJjDhtup75ow',
    );
    my $self = bless {
        twit => $twit
    },$class;
    return $self;
}


sub mention{
    $self = shift;
    my $twit = $self->{ twit };
    my $mentions = $twit->mentions();
    return $mentions;
}


sub get_tweet_id{
    my $self = shift;
    my $mentionsref = $self->mention;
    my @tweet_id = ();
    for my $mentions_list ( @$mentionsref ){
        push ( @tweet_id = $mentions_list->{ id } );
    }
    my $tweet_id_ref = \@tweet_id;
    return $tweet_id_ref;
}


sub connect_db{
    my $self = shift;
    my $dbh = DBI->connect("DBI:mysql:database=twitter;host=localhost","yasumoto","48FnSrCX", {'RaiseError' => 1});
    return $dbh;
}


sub get_saved_id{
    my $self = shift;
    my @saved_id = ();
    my $sth = $dbh->prepare("select tweet_id from mention");  #uekara20kenn;
    $sth->execute();
    while (my @row = $sth->fetchrow_array){
        push ( @saved_id = @row );
    }
        my $saved_id_ref = \@saved_id;
        $sth->finish;
    
    return $saved_id_ref;
}


sub relational_id{
    my $self = shift;
    my $tweet_id_ref = $self->get_tweet_id;
    my $saved_id_ref = $self->get_saved_id;
#sabunwotyekku 
    my @tweet_id = @$tweet_id_ref;
    my @saved_id = @$saved_id_ref;
    my @save_id = ();
    for my $tweet_id (@tweet_id){
        for my $saved_id (@saved_id){
            unless ( $saved_id  ==  $tweet_id ){
                 push ( @save_id = $tweet_id );
            }else{
            
            }
        }
    }
    my $save_id_ref = \@save_id;
    return $save_id_ref;
}


sub save_id{
    my $self = shift;
    my $save_id_ref = $self->relational_id;
    for my $mentions_list ( @$save_id_ref ){
        $tweet_id = $mentions_list->{ id };
        my $dbh = $self->connect_db;
        $dbh->do("insert into mention( tweet_id )values(\"$tweet_id\")");
    }
}


1;
